package com.blog.service.log.impl;

import com.blog.mapper.LogMapper;
import com.blog.model.Logger;
import com.blog.service.log.LogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户请求日志的业务接口实现类
 *
 * @author wmb
 *
 * @since 2021-2-8
 */
@Service("logService")
@Transactional
public class LogServiceImpl implements LogService {

    @Autowired
    @Qualifier("logMapper")
    private LogMapper logMapper;

    /**
     * 添加日志
     *
     * @param action
     * @param logData
     * @param ip
     * @param uid
     */
    @Override
    public void addLog(String action, String logData, String ip, Integer uid) {
        Logger logger = new Logger();
        logger.setAuthorId(uid);
        logger.setLogData(logData);
        logger.setIp(ip);
        logger.setAction(action);
        logMapper.saveLog(logger);
    }

    /**
     * 获取日志
     *
     * @return
     */
    @Override
    public PageInfo<Logger> getLoggers(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Logger> logList = logMapper.getLogs();
        return new PageInfo<>(logList);
    }
}
